<template>
  <div style="width: 100%">
    <a-card class="eip-admin-card-small" :bordered="false">
      <eip-search
        :option="table.search.option"
        @search="tableSearch"
      ></eip-search>
    </a-card>

    <a-card class="eip-admin-card-small">
      <vxe-toolbar
        ref="toolbar"
        custom
        print
        export
        :refresh="{ query: tableInit }"
      >
        <template v-slot:buttons>
          <eip-toolbar
            @del="del"
            @update="update"
            @add="add"
            @copy="copy"
            @desinger="desingerConfig"
            @onload="toolbarOnload"
          ></eip-toolbar>
        </template>
      </vxe-toolbar>
      <a-spin :spinning="table.loading">
        <vxe-table
          ref="table"
          id="workflowprocesslist"
          size="small"
          :seq-config="{
            startIndex: (table.page.param.current - 1) * table.page.param.size,
          }"
          :print-config="{}"
          :export-config="{}"
          :height="table.height"
          :data="table.data"
          :tooltip-config="{ showAll: true, contentMethod: tableTooltip }"
        >
          <vxe-table-column
            type="checkbox"
            width="60"
            align="center"
          ></vxe-table-column>
          <vxe-table-column
            type="seq"
            title="序号"
            width="60"
          ></vxe-table-column>
          <vxe-table-column
            field="name"
            title="名称"
            width="200"
            showOverflow="ellipsis"
          ></vxe-table-column>
          <vxe-table-column
            field="shortName"
            title="简称"
            width="200"
            showOverflow="ellipsis"
          ></vxe-table-column>
          <vxe-table-column field="icon" title="图标" width="60" align="center">
            <template v-slot="{ row }">
              <a-icon v-if="row.icon" :type="row.icon" :theme="row.theme" />
            </template>
          </vxe-table-column>
          <vxe-table-column
            field="typeName"
            title="类别"
            width="150"
          ></vxe-table-column>
          <!-- <vxe-table-column
            field="version"
            title="版本号"
            width="100"
          ></vxe-table-column> -->
          <vxe-table-column
            field="showLibrary"
            title="流程库显示"
            width="100"
            align="center"
          >
            <template v-slot="{ row }">
              <a-switch :checked="row.showLibrary" />
            </template>
          </vxe-table-column>
          <vxe-table-column
            field="isFreeze"
            title="冻结"
            width="100"
            align="center"
          >
            <template v-slot="{ row }">
              <a-switch :checked="row.isFreeze" @change="isFreezeChange(row)" />
            </template>
          </vxe-table-column>
          <vxe-table-column
            field="remark"
            title="备注"
            min-width="150"
            showOverflow="ellipsis"
          ></vxe-table-column>
          <vxe-table-column
            field="orderNo"
            title="排序"
            align="center"
            width="80"
          ></vxe-table-column>
          <vxe-table-column
            field="createUserName"
            title="创建人"
            width="100"
          ></vxe-table-column>
          <vxe-table-column
            field="createTime"
            title="创建时间"
            width="150"
          ></vxe-table-column>
          <vxe-table-column
            field="updateUserName"
            title="修改人"
            width="100"
          ></vxe-table-column>
          <vxe-table-column
            field="updateTime"
            title="修改时间"
            width="150"
          ></vxe-table-column>
          <vxe-table-column
            title="操作"
            align="center"
            fixed="right"
            width="160px"
          >
            <template #default="{ row }">
              <a-tooltip
                @click="tableUpdateRow(row)"
                title="编辑"
                v-if="visible.update"
              >
                <label class="text-eip eip-cursor-pointer">编辑</label>
              </a-tooltip>
              <a-divider type="vertical" v-if="visible.update" />
              <a-tooltip
                title="删除"
                v-if="visible.del"
                @click="tableDelRow(row)"
              >
                <label class="text-red eip-cursor-pointer">删除</label>
              </a-tooltip>
              <a-divider type="vertical" v-if="visible.desinger" />
              <a-tooltip
                title="设计"
                v-if="visible.desinger"
                @click="tableDesingerRow(row)"
              >
                <label class="eip-cursor-pointer">设计</label>
              </a-tooltip>
            </template>
          </vxe-table-column>
        </vxe-table>

        <a-pagination
          class="padding-top-sm float-right"
          v-model="table.page.param.current"
          size="small"
          show-size-changer
          show-quick-jumper
          :page-size-options="table.page.sizeOptions"
          :show-total="(total) => `共 ${total} 条`"
          :page-size="table.page.param.size"
          :total="table.page.total"
          @change="tableInit"
          @showSizeChange="tableSizeChange"
        ></a-pagination>
      </a-spin>
    </a-card>

    <edit
      ref="edit"
      v-if="edit.visible"
      :visible.sync="edit.visible"
      :data="edit.data"
      :title="edit.title"
      :copy="edit.copy"
      :processId="edit.processId"
      @ok="perateStatus"
    ></edit>
    <a-drawer
      width="100%"
      placement="right"
      :title="desinger.title"
      :visible="desinger.visible"
      :bodyStyle="desinger.bodyStyle"
      @close="desingerClose"
    >
      <desinger
        v-if="desinger.visible"
        ref="desinger"
        :processId="desinger.processId"
      ></desinger>
    </a-drawer>
  </div>
</template>

<script>
import {
  query,
  del,
  dictionaryQueryByIds,
} from "@/services/workflow/process/list";
import edit from "./edit";
import desinger from "./desinger";
import eipSearch from "@/pages/common/usercontrol/eip-search";
import eipToolbar from "@/pages/common/usercontrol/eip-toolbar";
import XEUtils from "xe-utils";
import { selectTableRow, deleteConfirm } from "@/utils/util";
export default {
  data() {
    return {
      typeTree: {
        style: {
          overflow: "auto",
          height: this.eipHeaderHeight() - 72 + "px",
        },
        data: [],
        spinning: true,
        right: {
          item: null,
          style: "",
        },
      },
      table: {
        page: {
          param: {
            current: 1,
            size: this.eipPage.size,
            sord: "asc",
            sidx: "process.OrderNo",
            filters: "",
            type: undefined,
          },
          total: 0,
          sizeOptions: this.eipPage.sizeOptions,
        },
        loading: true,
        data: [],
        height: this.eipHeaderHeight() - 156 + "px",
        search: {
          option: {
            num: 6,
            config: [
              {
                field: "Name",
                op: "cn",
                placeholder: "请输入名称",
                title: "名称",
                value: "",
                type: "text",
              },
              {
                field: "ShortName",
                op: "cn",
                placeholder: "请输入简称",
                title: "简称",
                value: "",
                type: "text",
              },
              {
                field: "Remark",
                op: "cn",
                placeholder: "请输入备注",
                title: "备注",
                value: "",
                type: "text",
              },
            ],
          },
        },
      },
      visible: {
        update: true,
        del: true,
        desinger: true,
      },
      edit: {
        visible: false,
        processId: "",
        copy: false,
        title: "",
      },

      desinger: {
        visible: false,
        processId: "",
        title: "",
        bodyStyle: {
          padding: "1px",
          "padding-bottom": "4px",
        },
      },
    };
  },
  components: { edit, desinger, eipSearch, eipToolbar },
  created() {
    this.tableInit();
    this.initTypeTree();
  },
  mounted() {
    this.$refs.table.connect(this.$refs.toolbar);
  },
  methods: {
    /**
     *
     */
    desingerClose() {
      this.desinger.visible = false;
    },

    /**
     * 初始化类型树
     */
    initTypeTree() {
      let that = this;
      dictionaryQueryByIds({
        id: that.eipEnum.流程类别,
      }).then((result) => {
        that.typeTree.spinning = false;
        that.typeTree.data = result.data;
      });
    },
    /**
     * 树图标
     */
    typeTreeIcon(props) {
      const { expanded } = props;
      if (props.children.length == 0) {
        return <a-icon type="file" />;
      }
      return <a-icon type={expanded ? "folder-open" : "folder"} />;
    },

    /**
     * 树选中
     */
    typeTreeSelect(electedKeys, e) {
      this.table.page.param.type = electedKeys[0];
      this.table.page.param.current = 1;
      this.tableInit();
    },
    /**
     * 设计
     */
    tableDesingerRow(row) {
      let that = this;
      that.desinger.processId = row.processId;
      that.desinger.title = "流程配置:" + row.name + " " + row.version;
      that.desinger.visible = true;
    },

    /**
     * 悬浮显示
     */
    tableTooltip({ type, column, row, items, _columnIndex }) {
      const { property } = column;
      if (row) {
        return row[property];
      }
      return null;
    },

    /**
     * 流程查询
     */
    tableSearch(filters) {
      this.table.page.param.current = 1;
      this.table.page.param.filters = filters;
      this.tableInit();
    },

    //初始化流程数据
    tableInit() {
      let that = this;
      that.table.loading = true;
      query(that.table.page.param).then(function (result) {
        if (result.code == that.eipResultCode.success) {
          that.table.data = result.data;
          that.table.page.total = result.count;
        }
        that.table.loading = false;
      });
    },
    /**
     *数量改变
     */
    tableSizeChange(current, pageSize) {
      this.table.page.param.size = pageSize;
      this.tableInit();
    },

    /**
     * 树更新
     */
    tableUpdateRow(row) {
      this.edit.processId = row.processId;
      this.edit.title = "流程配置-" + row.name;
      this.edit.visible = true;
    },
    /**
     * 删除
     */
    tableDelRow(row) {
      let that = this;
      deleteConfirm(
        "流程配置【" + row.name + "】" + that.eipMsg.delete,
        function () {
          that.$message.loading(that.eipMsg.delloading);
          del({ id: row.processId }).then((result) => {
            if (result.code == that.eipResultCode.success) {
              that.reload();
              that.$message.success(result.msg);
            } else {
              that.$message.error(result.msg);
            }
          });
        },
        that
      );
    },

    /**
     * 新增
     */
    add() {
      this.edit.copy = false;
      this.edit.processId = null;
      this.edit.title = "新增流程配置";
      this.edit.visible = true;
    },

    /**
     * 修改
     */
    update() {
      let that = this;
      selectTableRow(
        that.$refs.table,
        function (row) {
          that.edit.copy = false;
          that.edit.processId = row.processId;
          that.edit.title = "流程配置-" + row.name;
          that.edit.visible = true;
        },
        that
      );
    },
    /**
     * 复制
     */
    copy() {
      let that = this;
      selectTableRow(
        that.$refs.table,
        function (row) {
          that.edit.processId = row.processId;
          that.edit.copy = true;
          that.edit.title = "复制流程配置-" + row.name;
          that.edit.visible = true;
        },
        that
      );
    },
    /**
     * 预览
     */
    preview() {
      let that = this;
      selectTableRow(
        that.$refs.table,
        function (row) {
          that.edit.processId = row.processId;
          that.edit.title = "流程预览-" + row.name;
          that.edit.visible = true;
        },
        that
      );
    },

    /**
     * 删除
     */
    del() {
      let that = this;
      selectTableRow(
        that.$refs.table,
        function (rows) {
          //提示是否删除
          deleteConfirm(
            that.eipMsg.delete,
            function () {
              //加载提示
              that.$message.loading(that.eipMsg.delloading);
              let ids = XEUtils.map(rows, (item) => item.processId);
              del({ id: ids.join(",") }).then((result) => {
                that.$message.destroy();
                if (result.code == that.eipResultCode.success) {
                  that.reload();
                  that.$message.success(result.msg);
                } else {
                  that.$message.error(result.msg);
                }
              });
            },
            that
          );
        },
        that,
        false
      );
    },
    /**
     * 设计
     */
    desingerConfig() {
      let that = this;
      selectTableRow(
        that.$refs.table,
        function (row) {
          that.desinger.processId = row.processId;
          that.desinger.title = row.name;
          that.desinger.visible = true;
        },
        that
      );
    },

    /**
     * 编辑关闭
     */
    desingerCancel() {
      this.desinger.visible = false;
    },

    //提示状态信息
    perateStatus(result) {
      if (result.code === this.eipResultCode.success) {
        this.reload();
      }
    },

    /**
     * 重新加载
     */
    reload() {
      this.table.page.param.current = 1;
      this.tableInit();
    },

    /**
     * 权限加载完毕
     */
    toolbarOnload(buttons) {
      this.visible.desinger =
        buttons.filter((f) => f.method == "desinger").length > 0;
      this.visible.update =
        buttons.filter((f) => f.method == "update").length > 0;
      this.visible.del = buttons.filter((f) => f.method == "del").length > 0;
    },
  },
};
</script>

<style lang="less" scoped>
</style>
